package com.jdk.demo.queue.linkedTransferQueue;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TransferQueue;

public class Producer implements Runnable {

    private final TransferQueue<String> queue;

    public Producer(TransferQueue<String> queue) {
        this.queue = queue;
    }

    private String produce(int id) {
        return " your lucky number " + id;
    }

    @Override
    public void run() {
        int i = 1;
    	try {
            while (true) {
                if (queue.hasWaitingConsumer()) {
                    queue.put(produce(i++));
                }
                TimeUnit.SECONDS.sleep(1);// 生产者睡眠一秒钟,这样可以看出程序的执行过程
            }
        } catch (InterruptedException e) {
        }
    }

}